package com.cyh.core.framework.common.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Author: yanhang_chen
 * @Date: 2020/9/4 14:21
 */
public class MyStack<T> {

    private Stack<T> stack;

    public MyStack(){
        this.stack=new Stack<>();
    }

    public boolean isEmpty(){
        return this.stack.isEmpty();
    }

    public int Count(){
        return this.stack.size();
    }

    public T Top(){
        return this.stack.peek();
    }

    public void Push(T obj){
        stack.push(obj);
    }

    public T Pop(){
        return stack.pop();
    }

    public List<T> Pop(int count) throws NullPointerException {
        if(count>Count()){
            throw new NullPointerException("取出长度大于栈的长度!");
        }
        List<T> list=new ArrayList<>();
        while (count!=0){
            list.add(Pop());
            count--;
        }
        return list;
    }

    public List<T> get(int index,int count) throws NullPointerException {
        if((index+count)>Count()){
            throw new NullPointerException("复制长度大于栈的长度!");
        }
        List<T> list=CopyAsList();
        List<T> rlist=new ArrayList<>();
        for (int i = index;rlist.size()!=count; i++) {
            rlist.add(list.get(i));
        }
        return rlist;
    }

    public void RemoveTop(){
        stack.pop();
    }

    public int IndexOf(T obj){
        return stack.search(obj);
    }

    public List<T> CopyAsList(){
        if (Count()==0){
            return new ArrayList<>();
        }
        List<T> list=Pop(Count());
        for (int i = list.size()-1; i >=0 ; i--) {
            Push(list.get(i));
        }
        return list;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj==this){
            return true;
        }
        if(obj instanceof MyStack){
            MyStack myStack=(MyStack)obj;
            if(Count()!=myStack.Count()){
                return false;
            }else {
                List<T> list=this.CopyAsList();
                List<T> mlist=myStack.CopyAsList();
                int i=0;
                while (myStack.Count()!=0){
                    if(list.get(i)!=mlist.get(i)){
                        return false;
                    }
                    i++;
                }
                return true;
            }
        }else {
            return false;
        }
    }

}
